package edu.gdkm.collection;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class StudentManager {
    //Student[] students =new Student[100];
    //定义集合ArrayList，用于存放Student类型的对象
    List<Student> studentList =new ArrayList<Student>();
    //定义一个初始化方法，读取文件score中的信息
    public void init() throws IOException {
        File file = new File(".\\doc\\score.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String s;
        //读取一行数据，通过分隔符切出6段记录，分别放入Student这个实体类的对象中
        //最后将这个对象存入集合studentList中去
        while ((s = br.readLine()) != null) {
            String[] records = s.split(",");
            Student student = new Student();
            student.setSname(records[0]);
            student.setSnumber(records[1]);
            student.setScore1(Double.parseDouble(records[2]));
            student.setScore2(Double.parseDouble(records[3]));
            student.setScore3(Double.parseDouble(records[4]));
            student.setScore4(Double.parseDouble(records[5]));
            studentList.add(student);//将对象存入到集合中去
        }
    }
    /**
     * 1、设计方法，获取学生人数
     * */
    public int getStudentCount(){
        return studentList.size();
    }
    /**
     * 2、设计方法，输出所有学生信息
     * */
    public void printStudentList(){
//        for (int i = 0; i < studentList.size(); i++) {
//            System.out.println(studentList.get(i));
//        }
        //使用Iterator遍历集合,Iterator是集合遍历的专用接口，效率较高
        Iterator iterator=studentList.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    /**
     * 3、设计方法，通过学号查询学生信息
     * */
    public Student findStudentBySnumber(String snumber){
        for (int i = 0; i < studentList.size(); i++) {
            //i为下标，类似数组students[i]
            Student student = studentList.get(i);
            if(snumber.equals(student.getSnumber())){
                return student;
            }
        }
        return null;
    }
    //计算学生的总分
    public int computeScore(Student student){
        return (int)(student.getScore1()+student.getScore2()+student.getScore3()+student.getScore4());
    }
    //输出所有学生的总分
    public void pringStudentScore(){
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            System.out.println(student.getSname()+"的分数是:"+this.computeScore(student));
        }
    }
    //添加一个学生
    public void addStudent(Student student){
       studentList.add(student);
    }
    //获取学生最高分
    public Student getMaxScore(){
        int maxScore=0;
        Student maxScoreStudent=null;
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            if(maxScore<this.computeScore(student)){
                maxScore=this.computeScore(student);
                maxScoreStudent=student;
            }
        }
        return maxScoreStudent;
    }
}
